#include <mmu.h>
#include <memlayout.h>

    .globl _start
    .section ".start", "ax"
_start:
    j kern_entry
    .word 0xdeadbeef
    .align 3
    .global g_wake_up
    g_wake_up:
        .dword 1
        .dword 0

kern_entry:
    csrw mideleg, 0
    csrw medeleg, 0
    csrw mie, 0			// Mask all interrupts
    la t0, __alltraps
    csrw mtvec, t0
   
    csrr a0, mhartid		// get cpu id

    la sp, __stack_start__
    addi t1, a0, 1
    li t2, KSTACKSIZE
    mul t1, t1, t2
    add sp, sp, t1		// sp = (cupid+1) * KSTACKSIZE + __stack_start__

    bnez a0, secondary_cpu_entry
    j primary_cpu_entry


primary_cpu_entry:
    tail kern_init

secondary_cpu_entry:
    j secondary_cpu_entry
